// pages/jilu/jilu.js
const recorderManager = wx.getRecorderManager()
const app=getApp();

Page({
   
    /**
     * 页面的初始数据
     */
    data: {
        ifselect:false,
        isdrop:false,
        xinqing:'您的心情是',
        color:["#d78763","#e69dea","#99d38f","#8ed7c6",""],
        textcolor:"#cccccc",
        inputVal:"",
        value:"",
        text:"",
        Long_press:"长按录音",
        is_clock:false,
        hiddenmodalput:true,
        anyimage:"https://pic.imgdb.cn/item/6199aeca2ab3f51d91e29f6f.png",
        
        weather:[],
        care:[{
            "name":"开心",
            "color":"#d78763"
        },
        {
            "name":"难过",
            "color":"#e69dea"
        },
        {
            "name":"一般",
            "color":"#99d38f"
        },
        {
            "name":"愤怒",
            "color":"#8ed7c6"
        },
        {
            "name":"郁闷",
            "color":"#acc8ae"
        },
        {
            "name":"以上心情没有合适的",
            "color":"#f86a43"
        }
    ],
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        var that = this;
        let latitude;
        let longitude;
        this.setData({
            year: year,
            month: month,
            isToday: year +'年'+ month +"月"+ now.getDate()+"日",
            istime:year+''+month+''+now.getDate()
        })
        wx.getLocation({
            isHighAccuracy:true,
            success(res){
                latitude=res.latitude
                longitude=res.longitude
                that.getCity(latitude,longitude)
            }
        })
        wx.request({
            url: 'https://wx.xxmtcds.top:2333/user/record/'+this.data.isToday,
            header:{
              'authorization':app.globalData.token
            },
            success:function(res){
                console.log(res)
                if(res.data[0]){
                    that.setData({
                        text:res.data[0].record_content,
                        xinqing:res.data[0].record_mood,
                        voiceUrl:res.data[0].record_voice_url
                    })
                    that.start()
                }else{
                    return
                }
            }
        })
        
        
    },
    start(){
        var that=this;
        wx.downloadFile({
            url: this.data.voiceUrl, 
            success:(res)=> {
               console.log(res)
               if (res.statusCode === 200) {
                   this.innerAudioContext.src = res.tempFilePath;
                   this.innerAudioContext.play()
                   this.innerAudioContext.volume=0
                   this.innerAudioContext.onCanplay(()=>{
                        this.innerAudioContext.duration;
                        setTimeout(()=> {
                            that.setData({
                                voicel:this.innerAudioContext.duration
                            })
                        }, 1000);
                    })
                
               }
            }
        })
    },
    keji(e){
        this.setData({
            text1:e.data.now.text
        })
    },
    getCity(latitude,longitude){
        var that = this;
        let weizhi =longitude+","+latitude
        wx.request({
            url:"https://devapi.qweather.com/v7/weather/now?key=e294f967ee0440699e27ee0fbbca6b65&location="+weizhi,
            data:{},
            method:"GET",
            success:function(res){
                that.keji(res);
            }
        })
    },
    listall(){
        this.setData({
            ifselect:!this.data.ifselect,
            isdrop:!this.data.isdrop
        })
        console.log(this.data.isdrop)
    },
    modalinput(){
        this.setData({
            hiddenmodalput:!this.data.hiddenmodalput
        })
    },
    Select(e){
        if(e.currentTarget.dataset.name!="以上心情没有合适的"){
            var name = e.currentTarget.dataset.name
            var color = e.currentTarget.dataset.color
            this.setData({
                xinqing:name,
                ifselect:false,
                textcolor:color,
                isdrop:!this.data.isdrop
            })
        }else{
            this.setData({
                hiddenmodalput:!this.data.hiddenmodalput
            })
        }
        
    },
    deletetext(){
        this.setData({
            inputVal:this.data.inputVal.substring(0,this.data.inputVal.length-1)
        })
    },
    confirm(){
        if(this.data.value){
            this.setData({
                xinqing:this.data.value,
                value:"",
                ifselect:false,
                textcolor:"#fbc02d",
                hiddenmodalput:!this.data.hiddenmodalput
            })
        }else{
            wx.showToast({
                title:'不可为空',
                icon:'none',
                duration:1000
            })
        }
    },
    saveVal2(e){
        this.setData({
            text:e.detail.value
        })
    },

    saveVal1(e){
        this.setData({
            value:e.detail.value
        })
    },
    saveVal(e){
        this.setData({
            inputVal:e.detail.value
        })
    },
    plustext(){
        this.setData({
            text:this.data.inputVal,
            textcolor1:"black",
            inputVal:""
        })
    },
    alldelete(){
        this.setData({
            text:""
        })
    },
    handleRecordStart:function(e){
        this.setData({
            Long_press:"松开发送",
            is_clock:true,
            startPoint:e.touches[0],
            anyimage:"https://pic.imgdb.cn/item/6199aeca2ab3f51d91e29f76.png"
        })
        const options={
            duration:10000,
            sampleRate:16000,
            numberOfChannels:1,
            encodeBitRate:48000,
            format:'mp3'
        }
        recorderManager.start(options);
        wx.showToast({
          title: '正在录音，上划取消发送',
          icon:"none",
          duration:5000
        })
        console.log("zoule")
    },
  handleRecordStop:function(e){
      let that=this;
      recorderManager.stop()
      this.setData({
        Long_press:"长按录音",
        anyimage:"https://pic.imgdb.cn/item/6199aeca2ab3f51d91e29f6f.png"
      })
      console.log(this.data.is_clock)
      recorderManager.onStop((res)=>{
          if(this.data.is_clock==true){
              if(res.duration<2000){
                wx.showToast({
                title:'录音时间太短',
                icon:'none',
                duration:1000
                })
               }else{
                    const {tempFilePath} = res;
                    console.log(tempFilePath)
                    wx.showLoading({
                        title: '语音上传中',
                    })
                    setTimeout(function () {
                        wx.hideLoading()
                    }, 2000)
                    this.setData({tempFilePath:tempFilePath})
                    console.log(this.data.tempFilePath)
                    wx.uploadFile({
                        url:"https://wx.xxmtcds.top:2333/user/voice",
                        filePath:tempFilePath,
                        name:"voice",
                        header:{
                            'authorization':app.globalData.token
                        },
                        success:function(res){
                            that.save(res)
                        },
                            fail:function(res){
                                console.log(res.errMsg)
                            },
                        })
               }
          }else{
              wx.showToast({
                  title:'取消录音成功',
                  icon:'none',
                  duration:1000
              })
          }
               
      })      
  },
  load(){
    wx.request({
        url: 'https://wx.xxmtcds.top:2333/user/record',
        method:'post',
        data:{
            text:this.data.text,
            voice:this.data.voiceUrl,
            time:this.data.isToday,
            weather:this.data.text1,
            mood:this.data.xinqing
        },
        header:{
            'authorization':app.globalData.token
        },
        success:function(res){
            console.log(res)
            wx.showModal({
              title:'提示',
              content:'记录已保存'
            })
        }
      })

    },
 
  save(res){
      var that=this
    this.setData({
        voiceUrl:res.data,
        
    })
    wx.downloadFile({
        url: this.data.voiceUrl, 
        success:(res)=> {
           console.log(res)
           if (res.statusCode === 200) {
               that.setData({
                   res:res
               })
               this.innerAudioContext.src = this.data.res.tempFilePath;
               this.innerAudioContext.play()
               this.innerAudioContext.volume=0
               this.innerAudioContext.onCanplay(()=>{
                    this.innerAudioContext.duration;
                    setTimeout(()=> {
                        that.setData({
                            voicel:this.innerAudioContext.duration
                        })
                    }, 1000);
                })
            
           }
        }
    })

},
  handleTouchMove:function(e){
    if(Math.abs(e.touches[e.touches.length-1].clientY-this.data.startPoint.clientY)>25){
        this.setData({
            is_clock:false
        })
        wx.showToast({
            title:"松开手指，取消发送",
            icon:"none",
            duration:1000
        })
    }
  },

  control(){
    this.innerAudioContext.volume=1
    this.innerAudioContext.play();
    
  },
 

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.innerAudioContext =  wx.createInnerAudioContext()
        
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad6c2ab3f51d91e20c91.jpg'
            })
        }
        let children=wx.getStorageSync('children')
        if(children!==''){
            this.setData({
                children:wx.getStorageSync('children')
            })
        }else{
            this.setData({
                children:'https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171d6.png'
            })
        }
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})